{% extends 'layout.html' %}

{% block content %}
    <div class="container">
        <div style="margin: 5px 0;">
            <a href="{% url 'server_add' %}" class="btn btn-primary">添加</a>
        </div>
        <div>
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>主机名</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for item in queryset %}
                    <tr>
                        <td>{{ item.id }}</td>
                        <td>{{ item.hostname }}</td>
                        <td>
                            <a href="{% url 'server_edit' pk=item.pk %}">编辑</a>
                            <a href="javascript:void(0);"
                               onclick="removeRow(this,'{% url 'server_del' pk=item.id %}');">删除</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

{% endblock %}

{% block js %}
    <script type="text/javascript">
        function removeRow(ths, url) {
            var result = confirm('是否确定要删除？');
            if (result) {
                $.ajax({
                    url: url,
                    type: 'GET',
                    dataType: "JSON",
                    success: function (arg) {
                        if (arg.status) {
                            $(ths).parent().parent().remove();
                        }
                    }
                })
            }
        }
    </script>
{% endblock %}
